
//
//  ST_BTCaptchaView.swift
//  BTVideo
//
//  Created by DouLei on 2019/7/9.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift

class ST_BTCaptchaView: UIView {
    
    static let numberCount = 4
    
    static let height = 70.cgFloat
    
    static let itemWidth = 50.cgFloat
//    MARK: - Properties
    private var labelViews = [UILabel]()
//    暂时注释
//    private var lineViews = [UIView]()
    
    lazy var textField:UITextField = {
        return UITextField()
    }()

//    var inputComplete:((String)->())?
    
//    输入完成回掉
    var inputTextStatus:((Bool)->())?
    
//    MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        for _ in 1...ST_BTCaptchaView.numberCount {
            k2create_text_label()
        }
        
        self.addSubview(textField)
        textField.snp.makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        textField.tintColor = Color.clear
        textField.textColor = Color.clear
        textField.keyboardType = UIKeyboardType.numberPad
        textField.delegate = self
        
        textField.addTarget(self, action: #selector(textChanged), for: UIControl.Event.editingChanged)
        
        
        
    }
    
    fileprivate let normalColor = Color.init(hexString: "#F2F4F7")!
    
    fileprivate let selectedColor = Color.init(hexString: "#42CEDA")!
    
    func k2create_text_label() {
        
        let control = UILabel()
        control.cornerRadius = 6
        control.textColor = Color.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.semibold)
        control.textAlignment = .center
        control.borderColor = Color.init(hexString: "#F2F4F7")
        control.borderWidth = 2
//        control.backgroundColor = Color.init(hexString: "#333333")
        
        self.addSubview(control)
        
//        let line = UIView()
//        line.backgroundColor = normalColor
//        self.addSubview(line)
        
        if let frontLabel = labelViews.last{
            
            control.snp.makeConstraints { (make) in
                make.height.equalTo(ST_BTCaptchaView.height)
                make.width.equalTo(ST_BTCaptchaView.itemWidth)
                make.left.equalTo(frontLabel.snp.right).offset(10)
                make.top.equalTo(self)
            }
            
        }else{
            
            control.snp.makeConstraints { (make) in
                make.height.equalTo(ST_BTCaptchaView.height)
                make.width.equalTo(ST_BTCaptchaView.itemWidth)
                make.left.equalTo(self)
                make.top.equalTo(self)
            }
        }
        
        self.labelViews.append(control)
        
//        line.snp.makeConstraints { (make) in
//            make.bottom.right.left.equalTo(control)
//            make.height.equalTo(2)
//        }
//
//        self.lineViews.append(line)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @objc func textChanged(){
    
            var intArray = [Int]()
            
            if let str = self.textField.text{
                
                intArray = str.map { (s) -> Int in
                    s.string.int ?? 0
                }
            }
            
            for (idx,item) in self.labelViews.enumerated() {
                
                if let i = intArray[safe:idx]{
                    item.text = i.string
                    
//                    self.lineViews[idx].backgroundColor = selectedColor
                    
                }else{
                    item.text = ""
                    
//                    self.lineViews[idx].backgroundColor = normalColor
                }
            }
            
            
//        if intArray.count == ST_BTCaptchaView.numberCount{
//                self.close_key_broad()
//        }

//                main_thread_execute(afterDelay: 0.25) {
//                    self.inputComplete?(intArray.map { String($0) }.joined())
//                }
//            }
        self.inputTextStatus?(intArray.count == ST_BTCaptchaView.numberCount)
        
    }
    
//    MARK: - Public Method
    func inputAction(){
        
        self.textField.becomeFirstResponder()
        
    }
    
    func close_key_broad(){
        
        self.textField.resignFirstResponder()
    }
    
}


extension ST_BTCaptchaView:UITextFieldDelegate{
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        let mutalbeStr = NSMutableString.init(string: textField.text ?? "")
        
        mutalbeStr.replaceCharacters(in: range, with: string)
        
        return mutalbeStr.length <= ST_BTCaptchaView.numberCount
    }
    
}
